cd "/Users/andreamatranga/Dropbox/Resubmission QJE/"



use World_Data.dta, clear
save World_Data.dta, replace

drop if agripres==0 | lagpres==1


collapse (mean) yearadop  tempmean tempmean2 prectmean2 tempdiff0 prectseas prectmean abslat abslat2  americas DIST plants pleasant, by(latitude longitude)
gen latgroup2= string(round(latitude/15.000001))
gen longroup2= string(round(longitude/15.000001))
generate geogroup = latgroup2 + longroup2
replace plants=0 if plants==.
rename yearadop dateadopted
label variable dateadopted "Agri Adoption, Years B.P."
label variable tempmean "Mean Temperature"
label variable prectmean "Mean Precipitation"


label variable prectseas "Precipitation Seas."
label variable tempdiff0 "Temperature Seas."
label variable abslat "Absolute Latitude"

label variable dateadopted "Agri Adoption, Years B.P."
label variable tempmean "Mean Temperature"
label variable prectmean "Mean Precipitation"

label variable plants "Domest. Plants"

label variable prectseas "Precipitation Seas."
label variable tempdiff0 "Temperature Seas."
label variable abslat "Absolute Latitude"
replace dateadopted = -dateadopted

save CrossectionData.dta, replace

*Table 2
use CrossectionData.dta, clear
drop if pleasant==0
eststo clear
eststo: quietly estpost summarize ///
   dateadopted tempdiff0 prectseas tempmean prectmean
   esttab, cells("mean sd min max") label nodepvar  
   
   esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/Table2.tex", replace cells("mean sd min max") label nodepvar  


*Table 3

   
use CrossectionData.dta, clear
*gen latgroup2= string(round(latitude/15.000001))
*gen longroup2= string(round(longitude/15.000001))
*generate geogroup = latgroup2 + longroup2

xi i.geogroup

replace DIST =DIST/1000000
gen DIST2 = DIST^2
label var DIST "Distance"
label var DIST2 "Distance^2"


eststo clear
drop if pleasant==0
*1only temperature
eststo: quietly reg dateadopted 	tempdiff0 , cluster(geogroup)
estadd scalar nclusters = e(N_clust), replace
*2only precipitation
eststo: quietly reg dateadopted 	prectseas, cluster(geogroup)
estadd scalar nclusters = e(N_clust), replace
*3both
eststo: quietly reg dateadopted 	tempdiff0 prectseas, cluster(geogroup)
estadd scalar nclusters = e(N_clust), replace
*4 + basic controls
eststo: quietly reg dateadopted 	tempdiff0 prectseas tempmean prectmean americas tempmean2 prectmean2  abslat, cluster(geogroup)
estadd scalar nclusters = e(N_clust), replace
eststo: quietly reg dateadopted 	tempdiff0 prectseas tempmean prectmean americas tempmean2 prectmean2  abslat DIST DIST2, cluster(geogroup)

estadd scalar nclusters = e(N_clust), replace
*7 + neighboorhood fixed effects (all obs)
*7 + neighboorhood fixed effects (all obs)
eststo: quietly reg dateadopted 	tempdiff0 prectseas tempmean prectmean abslat americas tempmean2 prectmean2 _Igeogroup*, cluster(geogroup)
eststo: quietly reg dateadopted 	tempdiff0 prectseas tempmean prectmean abslat americas tempmean2 prectmean2 DIST DIST2 _Igeogroup* , cluster(geogroup)
estadd scalar nclusters = e(N_clust), replace


esttab, b(%8.1f) r2 compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Temp Seas" "Precip Seas" "Basic" "Controls" "Controls+Dist"  "Neighbor FE"  "Neigh. + Dist" ) ///
  nocons  se label  stats(r2 N, fmt(%8.2f %8.0f %8.0f)) order(tempdiff0 prectseas tempmean prectmean abslat abslat2 americas) /// 
  indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" "Geographic FE = _Igeog*")  scalar(nclusters)

  
esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/Table3.tex", replace b(%8.1f) r2 compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Temp Seas" "Precip Seas" "Basic" "Controls" "Controls+Dist"  "Neighbor FE" "Neigh. + Dist") ///
  nocons  se label  stats(r2 N, fmt(%8.2f %8.0f %8.0f)) order(tempdiff0 prectseas tempmean prectmean abslat abslat2 americas) /// 
  indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" "Geographic FE = _Igeog*")  scalar(nclusters)
  

****Table 3

use World_Data.dta, replace 
gen latgroup2= string(round(latitude/15.000001))
gen longroup2= string(round(longitude/15.000001))
gen geogroup15 = latgroup2 + longroup2
generate period500= -int(time/500)*500

generate period2= int(time/2000)*2000

*drop agriduration
generate agriduration = agrifront
replace agriduration= agrifront+agriduration[_n-1]
sort cellid time

by cellid:gen tempdiff0m = tempdiff0[_N]
by cellid:gen prectseasm = prectseas[_N]
by cellid:gen tempmeanm = tempmean[_N]
by cellid:gen prectmeanm = prectmean[_N]

label variable tempdiff0m "Temp. Seas. Today"
label variable prectseasm "Precip. Seas. Today"
label variable tempmeanm "Temp. Mean Today"
label variable prectmeanm "Precip. Mean Today"



replace pleasant =0
replace pleasant = 1 if tempmean>0 & prectmean >0.5

egen geogroup500 = group(period500 geogroup)
generate time2 = time^2
generate time3 = time^3
 tab geogroup15, gen(geogroupfe)
label variable time "Year"
label variable time2 "Year Squared"
label variable newinvev7s  "Agri. Invention"

 
save PanelDataReady.dta, replace 
*USE THIS ONE

local options "vce(cluster geogroup15)"


local timefe "i.period500"
local geogroupfe "geogroupfe*"


drop if pleasant==0

eststo clear
*Only Temps Seas
eststo: quietly logit newinvev7s 	tempdiff0    	      tempmean prectmean abslat  if lagpres == 0, `options'
*Only Precip Seas
eststo: quietly logit newinvev7s 			   prectseas  tempmean prectmean abslat  if lagpres == 0, `options'
*3 Basic
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat  if lagpres == 0, `options'

*4 With time fixed effects
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat `timefe'  if lagpres == 0, `options'


*5 adding extra controls 
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 if lagpres == 0, `options'
*6 with time FE
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 `timefe' if lagpres == 0, `options'

*7 With modern weather
eststo: quietly logit newinvev7s 	tempdiff0 tempdiff0m prectseas prectseasm  tempmean prectmean abslat   americas  if lagpres == 0,   `options'

esttab, b(%8.3f)  eqlabel("") label  eform compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Temp+Basic" "Precip+Basic" "Both+Basic" "Basic+500y FE" "Extra Controls" /// 
 "\shortstack{Extra Controls +\\ 500y FE}"  "Modern Weather" )   nocons  se ///
 stats( r2_p N, fmt(%8.2f %8.0f) labels(`"Pseudo-R2"'`"N"')) order(tempdiff0 prectseas tempmean prectmean abslat abslat2 americas) ///
indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" "500yr FE = *period*")

esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/Table4.tex" , replace b(%8.3f)  eqlabel("") label  eform compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Temp+Basic" "Precip+Basic" "Both+Basic" "Basic+500y FE" "Extra Controls" /// 
 "\shortstack{Extra Controls +\\ 500y FE}"  "Modern Weather" )   nocons  se ///
 stats( r2_p N, fmt(%8.2f %8.0f) labels(`"Pseudo-R2"'`"N"')) order(tempdiff0 prectseas tempmean prectmean abslat abslat2 americas) ///
indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" "500yr FE = *period*")

 
*Calculate margins for the 
logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 if lagpres == 0, `options'

margins, at(tempdiff0=(0 12 20 33 )) post
logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 if lagpres == 0, `options'

margins, at(prectseas=(0 1.5 2.3 3.4 )) post



***Table 5


cd "/Users/andreamatranga/Dropbox/Resubmission QJE"


use WesternEurasia.dta, replace
label var tempdiff0 "Temp. Seas."
label var prectseas "Precip. Seas."

tab code, gen(countrydums)
 
 
 
 label var tempdiff0 "Temp. Seas."
label var prectseas "Precip. Seas."
label var yearadop "Year Adopted"
eststo clear
eststo: quietly reg yearadop tempdiff0 prectseas prectmean tempmean tempmean2 prectmean2 latitude distorig altitude, cluster(code)
eststo: quietly reg yearadop  prectmean tempmean tempmean2 prectmean2 latitude distorig avg_sd_ia avg_sd_ia_s altitude, cluster(code)
eststo: quietly reg yearadop tempdiff0 prectseas prectmean tempmean tempmean2 prectmean2 latitude distorig avg_sd_ia avg_sd_ia_s altitude, cluster(code)
eststo: quietly reg yearadop sd_ia_spr sd_ia_spr_s   tempdiff0 prectseas prectmean tempmean tempmean2 prectmean2 latitude distorig altitude, cluster(code)
eststo: quietly reg yearadop sd_ia_sum sd_ia_sum_s   tempdiff0 prectseas prectmean tempmean tempmean2 prectmean2 latitude distorig altitude, cluster(code)
eststo: quietly reg yearadop sd_ia_aut sd_ia_aut_s   tempdiff0 prectseas prectmean tempmean tempmean2 prectmean2 latitude distorig altitude, cluster(code)
eststo: quietly reg yearadop sd_ia_win sd_ia_win_s   tempdiff0 prectseas prectmean tempmean tempmean2 prectmean2 latitude distorig altitude, cluster(code)

esttab, r2 se compress star(* 0.1 ** 0.05 *** 0.01 ) nocons label indicate( "Controls=prectmean tempmean tempmean2 prectmean2 latitude  altitude distorig" ) mtitles("Seasonality" "Interannual Average" "Both" "IA Spring" "IA Summer"  "IA Autumn" "IA Winter")


esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/Table5.tex", replace r2 se compress star(* 0.1 ** 0.05 *** 0.01 ) nocons label indicate( "Controls=prectmean tempmean tempmean2 prectmean2 latitude  altitude distorig" ) mtitles("Seasonality" "Interannual Average" "Both" "IA Spring" "IA Summer"  "IA Autumn" "IA Winter")

 
 


************
* Table A1 PLANT
************
*Careful
* Effect  of  seasonality  on  date  of  agricultural  adoption,  controlling  for  the  presence  of domesticable species

*** Changes
* Georgy: 
* (1) add R2 ; 
* (2) cluster SEs everywhere;  
* (3) delete columns 5, 6 with seasonality index; ; 
* (4) More info on notes - type of errors, DVs and IDVs, regression estimator
* DONE GEORGY

cd "/Users/andreamatranga/Dropbox/Resubmission QJE/"
clear all
	use CrossectiondataSubmit2, clear

*xi i.geogroup
replace plants=0 if plants==.
generate tdplantint= tempdiff0*plants
label variable tdplantint "Temp. Seas. * Plants"
generate psplantint= prectseas*plants
label variable psplantint "Precip. Seas. * Plants"
generate tmplantint= tempmean*plants
label variable tmplantint  "Temp. Mean * Plants"
generate pmplantint= prectmean*plants
label variable pmplantint "Precip. Mean * Plants"



xi i.geogroup
drop if pleasant==0
 label var plant "Dom. Plants"
eststo clear

eststo: quietly reg dateadopted  	tempdiff0 prectseas   tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 plants, cluster(geogroup)
eststo: quietly reg dateadopted  	tempdiff0 prectseas tdplantint psplantint  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 plants, cluster(geogroup)
eststo: quietly reg dateadopted  	tempdiff0 prectseas tdplantint psplantint tmplantint pmplantint  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 plants, cluster(geogroup)


esttab,  r                                 b(%8.1f)compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Plants" "Seas. Interactions" "+ Means Interactions" "Quad" "Americas" ) ///
  nocons  se label  stats(r2 N, fmt(%8.2f %8.0f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) /// 
  indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" )
esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/TableA1.tex",  replace                                b(%8.1f)compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Plants" "Seas. Interactions" "+ Means Interactions" "Quad" "Americas" ) ///
  nocons  se label  stats(r2 N, fmt(%8.2f %8.0f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) /// 
  indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" )




***Table A2



cd "/Users/andreamatranga/Dropbox/Resubmission QJE/"
use World_Data.dta, replace 
gen latgroup2= string(round(latitude/15.000001))
gen longroup2= string(round(longitude/15.000001))
gen geogroup15= latgroup2 + longroup2





*twoway (scatter latitude longitude if time ==-8000 )(scatter latitude longitude if time ==-2000 )







gen sedentary = newinvev7s
gen locname=""


replace sedentary = 0 if cellstr=="150"
replace sedentary = 1 if cellstr=="150"& time==-12000
replace locname="Sahel" if cellstr=="150" 

replace sedentary = 0 if cellstr=="37.537.5"
replace sedentary = 1 if cellstr=="37.537.5" & time ==-14000
replace locname="Levant" if cellstr=="37.537.5"


replace sedentary = 0 if cellstr=="30116.25"
replace sedentary = 1 if cellstr=="30116.25" & time ==-12500
replace locname="S.China" if cellstr=="30116.25" 


replace sedentary = 0 if cellstr=="37.5-86.25"
replace sedentary = 1 if cellstr=="37.5-86.25" & time ==-9000
replace locname="N.America" if cellstr=="37.5-86.25"

replace locname="Mexico" if cellstr=="18.75-105"

replace locname="S.America" if cellstr=="-18.75-67.5"

replace locname="N.China" if cellstr=="37.5112.5"

  
save sedentary.dta,replace
drop if pleasant==0


eststo clear
eststo: quietly logit sedentary	tempdiff0  prectseas  i.cellid if lagpres == 0  , cluster(geogroup15)  
eststo: quietly logit sedentary	tempdiff0  prectseas  tempmean prectmean abslat i.cellid if lagpres == 0 , cluster(geogroup15)  
eststo: quietly logit sedentary	tempdiff0  prectseas  tempmean prectmean abslat abslat2  tempmean2 prectmean2  americas i.cellid if lagpres == 0 , cluster(geogroup15) 


esttab , replace   eqlabel("") b(%8.3f) ///
prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "\begin{tabular}{l*{3}{c}} \\" ///
 "\hline\hline" "&\multicolumn{2}{c}{Dependent variable: Sedentism Dummy} \\") ///
 nogap compress star(* 0.1 ** 0.05 *** 0.01) eform  mtitles("Basic" "Controls" "Controls2" ) ///
 nocons  se label  stats(N , fmt(%8.0f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) ///
 indicate( "Extra Controls = abslat2 americas tempmean2 prectmean2""CellID = *cellid*")

 
esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/TableA2.tex", replace   eqlabel("") b(%8.3f) ///
prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "\begin{tabular}{l*{3}{c}} \\" ///
 "\hline\hline" "&\multicolumn{2}{c}{Dependent variable: Sedentism Dummy} \\") ///
 nogap r2 compress star(* 0.1 ** 0.05 *** 0.01) eform  mtitles("Basic" "Controls" "Controls2" ) ///
 nocons  se label  stats(N , fmt(%8.0f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) ///
 indicate("Extra Controls = abslat2 americas tempmean2 prectmean2""CellID = *cellid*" )
 
 
sort cellid time


************
* Table A3
************

 cd "/Users/andreamatranga/Dropbox/Resubmission QJE/"
use CrossectionData.dta, clear

gen const=1
gen cutoff1=15
gen cutoff2=15
g cellid= _n


drop if dateadop==.
drop if latitude>55
drop if pleasant==0
set matsize 800
spatwmat, name(W2) xcoord(latitude) ycoord(longitude) band(0 15) binary eigenval(E2)
eststo clear
eststo: quietly reg 						dateadopted tempdiff0 prectseas tempmean prectmean abslat, robust
eststo: quietly reg 						dateadopted tempdiff0 prectseas tempmean prectmean abslat abslat2 americas tempmean2 prectmean2, cluster(geogroup)
eststo: quietly spatreg dateadopted tempdiff0 prectseas tempmean prectmean abslat , weights(W2) eigen(E2) model(error)
eststo: quietly spatreg 	dateadopted tempdiff0 prectseas tempmean prectmean tempmean2 prectmean2 abslat abslat2 americas, weights(W2) eigen(E2) model(error)
eststo: quietly ols_spatial_HAC dateadopted  tempdiff0  prectseas  tempmean prectmean abslat ///
  const, lat(latitude) lon(longitude) t(const) p(cellid) dist(500) lag(0) disp
eststo:quietly ols_spatial_HAC  dateadopted tempdiff0 prectseas tempmean prectmean tempmean2 prectmean2 abslat abslat2 americas  ///
  const , lat(latitude) lon(longitude) t(const) p(cellid) dist(500) lag(0) disp

esttab, b(%8.1f) r compress star(* 0.1 ** 0.05 *** 0.01)  mtitles("Basic" "Controls" "Basic Spat.Lag" "Controls Spat. Lag" "Basic Conley" "SpatLag Conley") /// 
nocons  se label  stats(r2 p N, fmt(%8.2f %8.2f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) /// 
indicate("Extra Controls = abslat2 americas tempmean2 prectmean2")

esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/TableA3.tex", b(%8.1f) r compress star(* 0.1 ** 0.05 *** 0.01)  mtitles("Basic" "Controls" "Basic Spat.Lag" "Controls Spat. Lag" "Basic Conley" "SpatLag Conley") /// 
nocons  se label  stats(r2 p N, fmt(%8.2f %8.2f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) /// 
indicate("Extra Controls = abslat2 americas tempmean2 prectmean2")





**************************************  
*Table A4   

 cd "/Users/andreamatranga/Dropbox/Resubmission QJE/"


use World_Data.dta, replace

gen latgroup15= string(round(latitude/15.000001))
gen longroup15= string(round(longitude/15.000001))
gen geogroup15 = latgroup15 + longroup15

drop if pleasant==0

save newinvev2pretime.dta, replace



eststo clear

use newinvev2pretime.dta, replace
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2  tempmean2 prectmean2 americas if lagpres == 0, cluster(geogroup15)  


save newinvev2pretime.dta, replace

use newinvev2pretime.dta, replace
generate period2= -int(time/500)*500
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2  tempmean2 prectmean2  americas i.period2 if lagpres == 0, cluster(geogroup15)  


use newinvev2pretime.dta, replace
generate period2= -int(time/1000)*1000
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2  tempmean2 prectmean2 americas i.period2 if lagpres == 0, cluster(geogroup15)  

use newinvev2pretime.dta, replace
generate period2= -int(time/2000)*2000
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2  tempmean2 prectmean2 americas i.period2 if lagpres == 0, cluster(geogroup15)  

use newinvev2pretime.dta, replace
generate period2= -int(time/5000)*5000
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2  tempmean2 prectmean2 americas i.period2 if lagpres == 0, cluster(geogroup15)  

esttab , eform replace  b(%8.3f) ///
prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "\begin{tabular}{l*{6}{c}} \\" ///
 "\hline\hline" "&\multicolumn{5}{c}{Dependent variable: invention dummy} \\") ///
 nogap compress star(* 0.1 ** 0.05 *** 0.01) mtitles("NO FEs" "500Y FEs" "1000Y FEs" "2000Y FEs" "5000Y FEs" "NO FEs" "500Y FEs" "1000Y FEs" "2000Y FEs" "5000Y FEs" ) ///
 nocons  se label  stats( p N, fmt(%8.2f %8.0f)) eqlabel(none) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) ///
 indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" "Period FE = *period*")

esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/TableA4.tex", eform replace  b(%8.3f) ///
prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "\begin{tabular}{l*{6}{c}} \\" ///
 "\hline\hline" "&\multicolumn{5}{c}{Dependent variable: invention dummy} \\") ///
 nogap compress star(* 0.1 ** 0.05 *** 0.01) mtitles("NO FEs" "500Y FEs" "1000Y FEs" "2000Y FEs" "5000Y FEs" "NO FEs" "500Y FEs" "1000Y FEs" "2000Y FEs" "5000Y FEs" ) ///
 nocons  se label  stats( p N, fmt(%8.2f %8.0f)) eqlabel(none) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) ///
 indicate("Extra Controls = abslat2 americas tempmean2 prectmean2" "Period FE = *period*")

 


 
 **********
 *Table A5

 cd "/Users/andreamatranga/Dropbox/Resubmission QJE/"

use World_Data.dta, replace  

gen latgroup7= string(round(latitude/7.000001))
gen longroup7= string(round(longitude/7.000001))
gen geogroup7 = latgroup7 + longroup7


gen latgroup15= string(round(latitude/15.000001))
gen longroup15= string(round(longitude/15.000001))
gen geogroup15 = latgroup15 + longroup15


gen latgroup30= string(round(latitude/30.000001))
gen longroup30= string(round(longitude/30.000001))
gen geogroup30 = latgroup30 + longroup30


sort cellid time

drop if pleasant==0

eststo clear

 
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat  if lagpres == 0, cluster(geogroup7)  
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat  if lagpres == 0, cluster(geogroup15)  
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat  if lagpres == 0, cluster(geogroup30)  
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 if lagpres == 0, cluster(geogroup7)  
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 if lagpres == 0, cluster(geogroup15)  
eststo: quietly logit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2 if lagpres == 0, cluster(geogroup30) 

esttab, eform b(%8.3f)  compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Clust.7deg" "Clust.15deg" "Clust.30deg"  "Ctrl+7deg" "Ctrl+15deg" "Ctrl+30deg" )   nocons  se label  stats( p N, fmt(%8.2f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) indicate("Extra Controls = abslat2 americas tempmean2 prectmean2")

esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/TableA5.tex", replace eform b(%8.3f)  compress star(* 0.1 ** 0.05 *** 0.01) mtitles("Clust.7deg" "Clust.15deg" "Clust.30deg"  "Ctrl+7deg" "Ctrl+15deg" "Ctrl+30deg" )   nocons  se label  stats( p N, fmt(%8.2f %8.0f)) order(tempdiff0 prectseas  tempmean prectmean abslat abslat2 americas) indicate("Extra Controls = abslat2 americas tempmean2 prectmean2")



*** Table A6


* Rare Events and Firth Logit
use World_Data.dta,replace
gen latgroup15= string(round(latitude/15.000001))
gen longroup15= string(round(longitude/15.000001))
gen geogroup15 = latgroup15 + longroup15
drop if pleasant==0
eststo clear
eststo: quietly relogit newinvev7s  tempdiff0 prectseas if lagpres == 0 ,cluster(geogroup15)
eststo: quietly relogit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat   if lagpres == 0 ,cluster(geogroup15)
eststo: quietly relogit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2  if lagpres == 0  ,cluster(geogroup15)
eststo: quietly firthlogit newinvev7s  tempdiff0 prectseas if lagpres == 0 ,
eststo: quietly firthlogit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat   if lagpres == 0 ,
eststo: quietly firthlogit newinvev7s 	tempdiff0  prectseas  tempmean prectmean abslat abslat2 americas tempmean2 prectmean2   if lagpres == 0  ,

esttab , b(%8.3f) compress eform star(* 0.1 ** 0.05 *** 0.01) mtitles("RELogit" "REL + Controls" "REL + Controls2"   "FirthLogit" "FL + Controls" "FL + Controls2"  ) ///
nocons  se label stats(  N, fmt( %8.0f)) order(tempdiff0 prectseas tempmean prectmean abslat abslat2 americas) /// 
indicate("Extra Controls = abslat2 americas tempmean2 prectmean2")


esttab using "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/TableA6.tex", replace b(%8.3f) compress eform star(* 0.1 ** 0.05 *** 0.01) mtitles("RELogit" "REL + Controls" "REL + Controls2"   "FirthLogit" "FL + Controls" "FL + Controls2"  ) ///
nocons  se label stats(  N, fmt( %8.0f)) order(tempdiff0 prectseas tempmean prectmean abslat abslat2 americas) /// 
indicate("Extra Controls = abslat2 americas tempmean2 prectmean2")



**** Figure 1


*Panel A
use World_Data.dta,replace
* Extract observations where newinvev7s == 1
keep if newinvev7s == 1
save onlyadopt.dta, replace

* Reload the original dataset
use World_Data.dta,replace
keep if pleasant==1
* Calculate the 90th, 95th, and 99th percentiles for each time period
egen pct50_tempdiff = pctile(tempdiff), by(time) p(50)
egen pct75_tempdiff = pctile(tempdiff), by(time) p(75)
egen pct90_tempdiff = pctile(tempdiff), by(time) p(90)
egen pct95_tempdiff = pctile(tempdiff), by(time) p(95)
collapse (first) pct50 pct75 pct90 pct95, by(time)

* Save the percentile data
save percentile_data.dta, replace
use percentile_data.dta, replace
append using onlyadopt.dta
* Merge the specific observations back into the percentile data using the new syntax

generate locname=""
replace locname="Sahel" if cellstr=="150" 


replace locname="Levant" if cellstr=="37.537.5"



replace locname="S.China" if cellstr=="30116.25" 


replace locname="N.America" if cellstr=="37.5-86.25"

replace locname="Mexico" if cellstr=="18.75-105"

replace locname="S.America" if cellstr=="-18.75-67.5"

replace locname="N.China" if cellstr=="37.5112.5"
scatter latitude longitude, mlabel(locname)



twoway (line pct50_tempdiff time, lcolor(gs1) title("Temperature Difference and Invention") legend(label(1 "50th Percentile") label(2 "75th Percentile") label(3 "90th Percentile") label(4 "95th Percentile") label(5 "Inventions"))) ///
       (line pct75_tempdiff time, lcolor(gs6)) ///
       (line pct90_tempdiff time, lcolor(gs1) lpatter(dash)) ///
       (line pct95_tempdiff time, lcolor(gs6) lpatter(dash)) ///
       (scatter tempdiff time if newinvev7s==1, mcolor(black) m(O) mlabel(locname)), ///
       ytitle("Temperature Difference") xtitle("Years Before Present")
graph save pct.gph ,replace


**** Panel B
use CrossectionData.dta, replace
generate dateadoptedr = round(-dateadopted/500)*500
collapse (mean) tempmean prectmean tempdiff prectseas , by(dateadopted) 
label variable tempmean "Temp. Mean"
label variable prectmean "Precip. Mean "
label variable tempdiff "Temp. Diff."
label variable prectseas "Precip. Seas"

label variable dateadopted "Year Adop."
 twoway (lfit tempdiff dateadopted,lcolor(gs4) )  (scatter tempdiff dateadopted ,mc(black)   )	,	ytitle("Temperature Difference")legend(off)  title(Temperature Difference and Adoption) xlabel(-12000(2000)0) xtitle("Years Before Present")

graph save bindate.gph ,replace
graph combine pct.gph bindate.gph, cols(2) ysize(8) xsize(16) iscale(1)
graph export "/Users/andreamatranga/Dropbox/Resubmission QJE/FinalGraphs/Figure1.pdf",replace 



*Figure 4

use World_Data.dta,replace


gen seastemp=1 if tempdiff0>22
gen seasprecip=1 if prectseas>2

local timeoption " -12000"

twoway  ///
    (scatter latitude longitude if time == `timeoption' & pleasant == 0, mcolor(gs12) msize(tiny) ) ///
    (scatter latitude longitude if time == `timeoption' & pleasant == 1, mcolor(gs8) msize(small)) ///
    (scatter latitude longitude if (time == `timeoption') & pleasant == 1 & seastemp == 1, msymbol(square) mcolor(black) msize(small)) /// 
    (scatter latitude longitude if (time == `timeoption') & pleasant == 1 & seasprecip == 1, mcolor(black) msize(small)) /// 
    (scatter latitude longitude if newinvev7s == 1, mc(white) ms(o) msize(huge)) ///
    (scatter latitude longitude if newinvev7s == 1, mc(black) ms(o) msize(vlarge)) ///
    (scatter latitude longitude if newinvev7s == 1, mc(white) ms(o)) ///
    , xlabels(-180 -90 0 90 180) yline(0, lcolor(black)) yline(23, lcolor(gs14)) yline(-23, lcolor(gs14)) yline(67, lcolor(gs14))   ///
      ylabels( -45 0 45) ///
      xlabel(-180(-90)180, add) ylabel(-45(45)90, add) /// 
      legend(order(1 2 3 4 6) label(1 "Too Cold or Dry") label(2 "Pleasant, not Seasonal") ///
             label(3 "Pleasant, High Temp. Seasonality") label(4 "Pleasant, High Precip. Seasonality") ///
              label(6 "Independent Invention") ///
             ) ///
      ytitle("Latitude") ///
      xtitle("Longitude") ///
      title("12k year BP")

graph export Figure4.pdf, replace
  
*Figure A1  
use sedentary.dta,replace

* note the actual figure from the paper was obtained by combining panels from both 
 xtset cellid time
generate newinvev7all =max(agripres, F5000.agripres, F10000.agripres,F15000.agripres,F20000.agripres)

  twoway (line tempdiff0 time if newinvev7all==1) (scatter tempdiff0 time if newinvev7s==1 & newinvev7all==1) /// 
(scatter tempdiff0 time if sedentary==1 & newinvev7all==1), by(locname,rescale legend(off) iscale(0.5)) 

twoway (line prectseas time if newinvev7all==1) (scatter  prectseas time if newinvev7s==1 & newinvev7all==1) /// 
(scatter  prectseas time if sedentary==1 & newinvev7all==1), by(locname,rescale legend(off) iscale(0.5) ) legend(off)


  
